package com.yinhaochuan.result;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class ExtractData {

	public String filepath;

	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}

	public ExtractData(String filepath) {
		this.filepath = filepath;

	}

	public void process() {
		
		String filepath = getFilepath();

		File file = new File(filepath);
		
		String outfilepath = filepath.substring(0,filepath.lastIndexOf("."))+ ".dat";
		
		BufferedReader br = null;
		OutputStreamWriter osw = null;
		
		String queryID = null;
		double d5 = 0;
		double d10 = 0;
		double d15 = 0;
		double d20 = 0;
		double d30 = 0;
		double d100 = 0;
		double d200 = 0;
		double d500 = 0;
		double d1000 = 0;

		
		try {
			osw = new OutputStreamWriter(new FileOutputStream(outfilepath,true), "GBK");
			
			String headline = "QueryID\t" + "5docs" + "\t" + "10docs" + "\t"
					+ "15docs" + "\t" + "20docs" + "\t" + "30docs" + "\t"
					+ "100docs" + "\t" + "200docs" + "\t" + "500docs" + "\t"
					+ "1000docs"+"\n";
			
			System.out.print(headline);
			osw.write(headline);
			osw.flush();
			
			br = new BufferedReader(new FileReader(file));
			String buffer = null;
			do {
				buffer = br.readLine();
				if (buffer != null) {
					if (buffer.startsWith("Queryid")) {
						queryID = buffer.substring(
								buffer.indexOf(":") + 1).trim();
					}
					if (buffer.contains("5 docs:")) {
						if (buffer.contains("15 docs:")) {
							d15 = Double.valueOf(buffer.substring(
									buffer.indexOf(":") + 1).trim());
						} else {

							d5 = Double.valueOf(buffer.substring(
									buffer.indexOf(":") + 1).trim());
						}
					}
					if (buffer.contains("10 docs:")) {
						d10 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					}

					if (buffer.contains("20 docs:")) {
						d20 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					}
					if (buffer.contains("30 docs:")) {
						d30 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					}
					if (buffer.contains("100 docs:")) {
						d100 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					}
					if (buffer.contains("200 docs:")) {
						d200 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					}
					if (buffer.contains("500 docs:")) {
						d500 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					}
					if (buffer.contains("1000 docs:")) {
						d1000 = Double.valueOf(buffer.substring(
								buffer.indexOf(":") + 1).trim());
					} else if (buffer.startsWith("R-Precision")) {
						String content =queryID+"\t"+ d5 + "\t" + d10 + "\t" + d15 + "\t"
								+ d20 + "\t" + d30 + "\t" + d100 + "\t" + d200
								+ "\t" + d500 + "\t" + d1000+"\n";
						System.out.print(content);
						osw.write(content);
						osw.flush();
					}
				}

			} while (buffer != null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) throws IOException {
		new ExtractData("./data/DMRFscore.rigid.txt").process();
	}

}
